【问题标题】:Implement Google Places into Google Maps Using My Current Location使用我的当前位置将 Google 地方信息应用到 Google 地图中
【发布时间】:2015-01-15 16:41:22
【问题描述】:

我一直在到处研究,但没有找到具体的例子来说明如何做到这一点。我想知道如何向 Google 发送 HTTP 请求,以带回存储在 LatLng 中的当前位置周围的数据?然后以 JSON 格式解析该数据,然后显示在我的地图上。我是android开发的新手,如果这个问题看起来很傻,我很抱歉。我已经查看了有关此的 Google 文档,但我无法理解它:(

我的 MainActivity.Java 文件:

package com.example.mapapp;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;


public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {

    GoogleMap map;
    LatLng myPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);

        LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();

        String provider = locationmanager.getBestProvider(criteria, true);
        Location location = locationmanager.getLastKnownLocation(provider);

        if(location!=null){
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            myPosition = new LatLng(latitude, longitude);

        }
    }

    @Override
    public void onMyLocationChange(Location location) {
        // TODO Auto-generated method stub

    }

}

我的 Manifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapapp"
    android:versionCode="1"
    android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />
  <permission 
    android:protectionLevel="signature" 
    android:name="com.example.mapapp.permission.MAPS_RECEIVE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-feature android:required="true" android:glEsVersion="0x00020000"/>

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library 
        android:name="com.google.android.maps"/>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data 
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data 
        android:name="com.google.android.maps.v2.API_KEY" 
        android:value="MY_API_KEY"/>    

  </application>

</manifest>

我的 Layout.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mapapp.MainActivity" >

     <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="250dp" 
        />   

    <fragment 
        android:id="@+id/map" 
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

【问题讨论】:

    标签: java android google-maps google-places


    【解决方案1】:

    如果您想显示附近的地点,您需要使用 Google Places API。

    您可以阅读this tutorial,了解如何在您的 Google Developer Console 中设置 API 项目。

    另外,您可以阅读this series of tutorials 了解如何显示附近的地点。

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 2016-02-26
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多