20.11.2016 Views

ANDROID APP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

android:apiKey="0ZDUMMY13442HjX491CODE44MSsJzfDVlIQ"<br />

/><br />

<br />

การใช้ Google Maps<br />

265<br />

ไฟล์ที่เราได้เตรียมไว้นั้นจะถูกนำมาใช้งานในหัวข้อถัดไป<br />

กรรมวิธี: การนำ Google Maps มาใช้ในแอพที่พัฒนาขึ้นเอง<br />

การแสดงแผนที่ของ Google ในแอพนั้น เราต้องเรียกใช้คำสั่ง MapActivity โดยแสดงไว้ใน<br />

ชุดคำสั่งที่ 10.10 และต้องกำหนดค่า ID ของเลย์เอาต์ที่จะแสดงแผนที่ด้วย ในที่นี้เราจะใช้ค่า map1<br />

และใช้เมธอด isRouteDisplayed() เพื่อแสดงผล ซึ่งผลลัพธ์ของชุดคำสั่งจะแสดงไว้ในรูปที่ 10.4<br />

ชุดคำสั่งที่ 10.10 src/com/cookbook/using_gmaps/MyLocation.java<br />

package com.cookbook.using_gmaps;<br />

import android.content.Context;<br />

import android.location.Criteria;<br />

import android.location.Location;<br />

import android.location.LocationManager;<br />

import android.os.Bundle;<br />

import android.widget.TextView;<br />

import com.google.android.maps.MapActivity;<br />

import com.google.android.maps.MapView;<br />

public class MyLocation extends MapActivity {<br />

LocationManager mLocationManager;<br />

Location mLocation;<br />

TextView tv;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

MapView mapView = (MapView) findViewById(R.id.map1);<br />

tv = (TextView) findViewById(R.id.tv1);<br />

mLocationManager = (LocationManager)<br />

getSystemService(Context.LOCATION_SERVICE);<br />

Criteria criteria = new Criteria();<br />

criteria.setAccuracy(Criteria.ACCURACY_FINE);<br />

criteria.setPowerRequirement(Criteria.POWER_LOW);<br />

String locationprovider =<br />

mLocationManager.getBestProvider(criteria,true);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!